#ifndef NETWORK_H
#define NETWORK_H

#include <unistd.h>
#include <string>

using std::string;

class socketError
{
public:
	socketError(const char* estring);
};

class tcpSocket
{
public:
	tcpSocket();
	tcpSocket(int fd, bool iscon);
	int Connect(const string &serAddr = "", unsigned short int port = 0);
	ssize_t Recv(void *buff, size_t len);
	ssize_t Recvn(void *buff, size_t len);
	ssize_t Send(const void *buff, size_t len);

	int getSocket() const	{	return sockfd; }
	bool isConnect() const	{	return connected;	}
	~tcpSocket();

private:
	int sockfd;
	bool connected;

	int Socket();
};

class tcpServer
{
public:
	tcpServer();
	bool Listen(unsigned short int port = 0, const string &localAddr = "");
	tcpSocket	waitForNewConnection();
	int getSocket() const { return sockfd; }

	~tcpServer();

private:
	static const int MAX_LISTEN = 5;
	int sockfd;
};

class udpClient
{
public:
	udpClient();
	~udpClient();
	bool Bind(const string &localAddr, unsigned short int port);
	ssize_t Recvfrom(void *buff, size_t len, string &remoteAddr, unsigned short int &remotePort);
	ssize_t Sendto(const void *buff, size_t len,const string &addr, unsigned short int port);
	int getSocket() const {return sockfd;}

private:
	int sockfd;
};

#endif //NETWORK_H
